﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Practices.Unity;
using System.IO;
using System.Windows.Annotations;
using System.Windows.Annotations.Storage;

namespace Skynet.C2SModule.Views.BookPrint
{
    /// <summary>
    /// Interaction logic for BookPrintView.xaml
    /// </summary>
    public partial class BookPrintView : UserControl, IBookPrintView
    {
        private Presenters.BookPrintPresenter _Presenter;
        [Dependency]
        public Presenters.BookPrintPresenter Presenter
        {
            get { return _Presenter; }
            set { 
                _Presenter = value;
                _Presenter.View = this;
                this.DataContext = value;
            }
        }

        private MemoryStream annotationStream;
        private AnnotationService service;

        public BookPrintView()
        {
            InitializeComponent();
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            this.Presenter.OnViewReady();
            service = new AnnotationService(dvBook);
            annotationStream = new MemoryStream();
            AnnotationStore store = new XmlStreamStore(annotationStream);
            service.Enable(store);
        }

        private void UserControl_Unloaded(object sender, RoutedEventArgs e)
        {
            if (service != null && service.IsEnabled)
            {
                // Flush annotations to stream.
                service.Store.Flush();
                // Disable annotations.
                service.Disable();
                annotationStream.Close();
            }
        }
    }
}
